package com.bwie.system.consumer;

/**
 * @ClassName:
 * @Description:
 * @Author: zengfanzhen
 * @Date: 2024/4/12 19:33:37
 */
import com.bwie.common.domain.Data1;
import com.bwie.common.domain.Data2;
import com.bwie.common.domain.Data3;
import com.bwie.system.mapper.Data1Mapper;
import com.bwie.system.mapper.Data2Mapper;
import com.bwie.system.mapper.Data3Mapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component
@Slf4j
public class DataConsumer {

    @Autowired // 自动注入Data1Mapper实例
    private Data1Mapper data1Mapper;

    @Autowired // 自动注入Data2Mapper实例
    private Data2Mapper data2Mapper;

    @Autowired // 自动注入Data3Mapper实例
    private Data3Mapper data3Mapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 监听数据1的队列
    @RabbitListener(bindings = {@QueueBinding(value = @Queue("data1"),
            exchange = @Exchange(value = "dandong", type = ExchangeTypes.DIRECT),
            key = "data1")})
    public void consumeData1(String message) {
        Data1 data1 = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // 将接收到的消息解析为Data1对象
            data1 = objectMapper.readValue(message, Data1.class);

            // 根据ID判断数据是否存在
            Data1 existingData1 = data1Mapper.selectById(data1.getId());
            if (existingData1 != null) {
                // 如果存在，则修改数据
                data1Mapper.update(data1);
                log.info("成功消费并更新Data1数据：{}", data1);
            } else {
                // 如果不存在，则插入数据
                data1Mapper.insert(data1);
                log.info("成功消费并插入Data1数据：{}", data1);
            }
        } catch (DuplicateKeyException e) {
            log.error("消费Data1数据失败：数据已存在，ID为 {}", data1 != null ? data1.getId() : "unknown");
        } catch (Exception e) {
            log.error("消费Data1数据失败：{}", e.getMessage());
        }
    }


    // 监听数据2的队列
    @RabbitListener(bindings = {@QueueBinding(value = @Queue("data2"),
            exchange = @Exchange(value = "dandong", type = ExchangeTypes.DIRECT),
            key = "data2")})
    public void consumeData2(String message) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // 将接收到的消息解析为Data2对象
            Data2 data2 = objectMapper.readValue(message, Data2.class);

            // 根据ID判断数据是否存在
            Data2 existingData2 = data2Mapper.selectById(data2.getId());
            if (existingData2 != null) {
                // 如果存在，则修改数据
                data2Mapper.update(data2);
                log.info("成功消费并更新Data2数据：{}", data2);
            } else {
                // 如果不存在，则插入数据
                data2Mapper.insert(data2);
                log.info("成功消费并插入Data2数据：{}", data2);
            }
        } catch (Exception e) {
            log.error("消费Data2数据失败：{}", e.getMessage());
        }
    }


    // 监听数据3的队列
    @RabbitListener(queues = "data3")
    public void consumeData3(String message) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // 将接收到的消息解析为Data3对象
            Data3 data3 = objectMapper.readValue(message, Data3.class);

            // 根据ID判断数据是否存在
            Data3 existingData3 = data3Mapper.selectById(data3.getId());
            if (existingData3 != null) {
                // 如果存在，则修改数据
                data3Mapper.update(data3);
                log.info("成功消费并更新Data3数据：{}", data3);
            } else {
                // 如果不存在，则插入数据
                data3Mapper.insert(data3);
                log.info("成功消费并插入Data3数据：{}", data3);
            }
        } catch (Exception e) {
            log.error("消费Data3数据失败：{}", e.getMessage());
        }
    }





}

